using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FinanceStats.Entities;

namespace FinanceStats.Business.Components
{
    public delegate void OutcomeDayDblClickedHandler(OutcomeDay day);
    
    public partial class OutcomeWeekControl : UserControl
    {
        public event OutcomeDayDblClickedHandler OutcomeDayDblClicked;

        private List<OutcomeDay> outcomeWeek;

        private List<Label> panelLabels;

        public List<OutcomeDay> OutcomeWeek
        {
            get { return outcomeWeek; }
            set
            {
                outcomeWeek = value;
                ShowWeekly();
            }
        }


        public OutcomeWeekControl()
        {
            InitializeComponent();
            InitializePanels();
        }

        public OutcomeWeekControl(List<OutcomeDay> week)
        {
            InitializeComponent();
            InitializePanels();
            this.outcomeWeek = week;
            ShowWeekly();
        }

        private void InitializePanels()
        {
            this.panelLabels = new List<Label>();
            this.tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetPartial;
            Font f = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            for (int i = 0; i < 7; i++)
            {
                Panel pnl = new Panel();
                pnl.Dock = DockStyle.Fill;
                pnl.Padding = new Padding(5);
                Label lbl = new Label();
                lbl.Font = f;
                lbl.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
                lbl.ForeColor = System.Drawing.SystemColors.MenuHighlight;
                lbl.Text = ((DayOfWeek)((i + 1) % 7)).ToString();
                pnl.Controls.Add(lbl);
                lbl = new Label();
                lbl.AutoSize = false;
                lbl.Name = "" + i;
                lbl.Location = new Point(5, 20);
                lbl.Width = pnl.Width - 10;
                lbl.Height = pnl.Height - 30;
                lbl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
                lbl.Font = f;
                lbl.ForeColor = System.Drawing.SystemColors.MenuHighlight;
                lbl.MouseEnter += new EventHandler(lbl_MouseEnter);
                lbl.MouseLeave += new EventHandler(lbl_MouseLeave);
                lbl.MouseDoubleClick += new MouseEventHandler(lbl_MouseDoubleClick);
                pnl.Controls.Add(lbl);
                this.tableLayoutPanel1.Controls.Add(pnl, i, 0);
                this.panelLabels.Add(lbl);
            }
        }

        private void ShowWeekly()
        {
            string textTemplate = @"
Total items:
{0}
Total spent:
{1}
";
            decimal total = 0;
            for (int i = 0; i < 7; i++)
            {
                OutcomeDay day = outcomeWeek[i];
                Label lbl = panelLabels[i];
                lbl.Text = String.Format(textTemplate, day.OutcomeList.Count, day.Total.ToString());
                total += day.Total;
            }
            label1.Text = String.Format("Week overview from {0} to {1}", outcomeWeek[0].Date.ToString("dd/MM/yyyy"), outcomeWeek[6].Date.ToString("dd/MM/yyyy"));
            label2.Text = String.Format("Total : {0}", total);
        }

        void lbl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.OutcomeDayDblClicked != null)
            {
                string name = ((Label)sender).Name;
                int index = Convert.ToInt32(name);
                OutcomeDay day = outcomeWeek[index];
                this.OutcomeDayDblClicked(day);
            }
        }

        void lbl_MouseLeave(object sender, EventArgs e)
        {
            Panel p = ((Label)sender).Parent as Panel;
            p.BackColor = System.Drawing.Color.White;
        }

        void lbl_MouseEnter(object sender, EventArgs e)
        {
            Panel p = ((Label)sender).Parent as Panel;
            p.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
        }

    }
}
